﻿Imports Script.Serialization
Imports MySoft.Map.Application
Imports System.Collections.Generic

Partial Public Class LogonModeSetting
    Inherits Mysoft.Map.Application.Security.AppPage

    Private JSon As New Script.Serialization.JavaScriptSerializer()
    Private ReturnFunction As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objReturn

        '获取传送参数
        Dim strType As String = Request.QueryString("p_businessType")     '业务标识
        Dim b(Request.InputStream.Length - 1) As Byte
        Request.InputStream.Read(b, 0, Request.InputStream.Length)
        Dim postData = System.Text.Encoding.UTF8.GetString(b) '提交数据

        ReturnFunction = Request.QueryString("ReturnFunction")

        '调用过程的返回结果为JSON对象
        Select Case strType.ToLower()

            Case "GetRSAKey".ToLower() '获取RSA密钥
                objReturn = GetRSAKey()
                Exit Select

            Case "SaveSetting".ToLower() '保存配置
                objReturn = SaveSetting(Request.QueryString("myset"))
                Exit Select

            Case "GetDomain".ToLower() '获取一级域名
                objReturn = GetDomain()
                Exit Select

            Case "CheckDSSSite".ToLower() '检测站点
                objReturn = CheckDSSSite()
                Exit Select

            Case Else
                Exit Select
        End Select

        MyBase.Response.ContentType = "text/HTML"
        MyBase.Response.Clear()
        If ReturnFunction <> "" Then
            MyBase.Response.DisableKernelCache()
            MyBase.Response.Cache.SetNoStore()
            MyBase.Response.Write(ReturnFunction + "('")
            MyBase.Response.Write(JSon.Serialize(objReturn))
            MyBase.Response.Write("')")
        Else
            MyBase.Response.Write(JSon.Serialize(objReturn)) '通过JSON对象解析成字符串返回至前台页面
        End If
        MyBase.Response.End()
    End Sub

    Private Function GetRSAKey() As Object
        Dim sRtn As New Dictionary(Of String, Object)

        Try
            Dim isCreate = Request.QueryString("isCreate")
            Dim rtn As IDictionary
            If String.Compare(isCreate, "true", True) = 0 Then
                rtn = Mysoft.Map.Security.RSAUtil.GetRSAKey(True) '生成新Key
            Else
                '还原密钥先从web.config中读取
                Dim sPrivateKey As String = WebConfig.ReadAppSettings("PrivateKey")
                Dim sPublicKey As String = WebConfig.ReadAppSettings("PublicKey")
                '若web.config中没有，则取程序默认密钥
                If sPublicKey = "" Then
                    rtn = Mysoft.Map.Security.RSAUtil.GetRSAKey(False) '取默认Key
                Else
                    rtn = New Dictionary(Of String, Object)
                    rtn.Add("PublicKey", sPublicKey)
                    rtn.Add("PrivateKey", sPrivateKey)
                End If

            End If
            sRtn.Add("result", True)
            sRtn.Add("PublicKey", rtn("PublicKey").ToString())
            sRtn.Add("PrivateKey", rtn("PrivateKey").ToString())
        Catch ex As Exception
            sRtn.Add("result", False)
            sRtn.Add("message", ex.Message)
        End Try

        Return sRtn
    End Function

    Private Function GetDomain() As Object
        Dim sRtn As New Dictionary(Of String, Object)
        Try
            sRtn.Add("DomainName", Mysoft.Map.Utility.General.GetDomain(Request.Url.ToString()))
            sRtn.Add("result", True)
        Catch ex As Exception
            sRtn.Add("result", False)
            sRtn.Add("message", ex.Message)
        End Try
        Return sRtn
    End Function

    Private Function SaveSetting(ByVal postData As String) As Object
        Dim sRtn As New Dictionary(Of String, Object)
        Try
            Dim obj = JSon.DeserializeObject(postData)
            WebConfig.WriteAppSettings("LOGON_MODE", obj("LOGON_MODE")) '登录模式

            'AD集成相关设置
            WebConfig.WriteAppSettings("IIS_PASS", obj("IIS_PASS"))

            'Cookie集成相关设置
            WebConfig.WriteAppSettings("DomainName", obj("DomainName"))
            '私钥为空，表示没有自定义密钥对
            If obj("PrivateKey") <> "" Then
                WebConfig.WriteAppSettings("PrivateKey", obj("PrivateKey"))
                WebConfig.WriteAppSettings("PublicKey", obj("PublicKey"))
            Else
                WebConfig.WriteAppSettings("PrivateKey", "")
                WebConfig.WriteAppSettings("PublicKey", "")
            End If

            sRtn.Add("result", True)
        Catch ex As Exception
            sRtn.Add("result", False)
            sRtn.Add("message", ex.Message)
        End Try
        Return sRtn
    End Function

    Private Function CheckDSSSite() As Object
        Dim sRtn As New Dictionary(Of String, Object)
        Try
            sRtn.Add("result", True)
        Catch ex As Exception
            sRtn.Add("result", False)
            sRtn.Add("message", ex.Message)
        End Try
        Return sRtn
    End Function

End Class